package com.nCoV.EpidemicSurveillance.scheduled;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nCoV.EpidemicSurveillance.common.utils.DateUtil;
import com.nCoV.EpidemicSurveillance.common.utils.ListUtil;
import com.nCoV.EpidemicSurveillance.common.utils.UUIDUtils;
import com.nCoV.EpidemicSurveillance.mapper.TjMapper;
import com.nCoV.EpidemicSurveillance.repository.PersonRepository;
import com.nCoV.EpidemicSurveillance.repository.TongJiRepository;

/**
 * @author 罗穆健
 * @date 2020年4月27日
 */
@Service
public class Tjfx {
	
	@Autowired
	PersonRepository personRepository;

	@Autowired
	TongJiRepository tongJiRepository;
	
	@Autowired 
	TjMapper tjMapper;
	Logger logger;
	public void tjfx() {
		List<Map<String, Object>> personList = new ArrayList<Map<String,Object>>();
		int qz = 0;//感染人数
		int gfx = 0;//高风险人数
		int zy = 0;//治愈人数
		try {
			personList = personRepository.getAllListJd();
			for (Map<String, Object> map : personList) {
				System.out.println(map);
				if(map.get("grgl") == null || "".equals(map.get("grgl").toString())) {
				}
				else if(Double.parseDouble(map.get("grgl").toString()) == 1.0) {
					qz++;
				}else if(Double.parseDouble(map.get("grgl").toString()) >= 0.5) {
					gfx++;
				}
				if(map.get("zlqk") == null || "".equals(map.get("zlqk").toString())) {
				}else if("zy".equals(map.get("zlqk").toString())) {
					zy++;
				}
			}
			tongJiRepository.updateTj("tongji", qz, gfx, zy, DateUtil.getNowDay());
		} catch (Exception e) {
			// TODO: handle exception
			logger.info(e);
		}
	}
	/**
	 * 删除
	 * @author 罗穆健
	 * @date 2020年4月30日
	 */
	public void scRelation() {
		List<Map<String, Object>> personList = new ArrayList<Map<String,Object>>();
		try {
			personList = personRepository.getAllListSc();
			System.out.println(personList);
			for (Map<String, Object> map : personList) {
				personRepository.deleteRelationSc(Long.parseLong(map.get("id").toString()));
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.info(e);
		}
	}
	
	public void tjChengShi() {
		List<Map<String, Object>> personList = new ArrayList<Map<String,Object>>();
		Map<String, Map<String, Integer>> personMap = new HashMap<String, Map<String, Integer>>();
		try {
			personList = personRepository.getAllListAddress();
			System.out.println(personList);
			for (Map<String, Object> map : personList) {
				System.out.println(map);
				if(map.get("zlqk") == null || "".equals(map.get("zlqk").toString())) {
				}else if("zy".equals(map.get("zlqk").toString())) {
					mapPut(map,personMap,"zy");
				}
				if(map.get("grgl") == null || map.get("grgl").toString()=="") {
					continue;
				} else if (Double.parseDouble(map.get("grgl").toString()) == 1.0) {
					mapPut(map,personMap,"qz");
				} else if (Double.parseDouble(map.get("grgl").toString()) >= 0.5) {
					mapPut(map,personMap,"gfx");
				}
				
				
			}
			System.out.println(personMap);
			String nowDay = DateUtil.getNowDay();
			for (Entry<String, Map<String, Integer>> entry : personMap.entrySet()) {
				String uuid = UUIDUtils.getUUID();
			    String [] addrspt = entry.getKey().split("\\|");
			    if(addrspt.length == 2) {
			    	if("- 请选择 -".equals(addrspt[1])) {
			    		addrspt[1] = "未明确地区";
			    	}
			    	tjMapper.insertCityTj(uuid,addrspt[0],addrspt[0],addrspt[1],
			    		 entry.getValue().get("qz"),entry.getValue().get("gfx"),
			    		 entry.getValue().get("zy"),nowDay);
			    } else {
			    	if("- 请选择 -".equals(addrspt[2])) {
			    		addrspt[2] = "未明确地区";
			    	}
			    	tjMapper.insertCityTj(uuid,addrspt[0],addrspt[1],addrspt[2],
			    		entry.getValue().get("qz"),entry.getValue().get("gfx"),
			    		entry.getValue().get("zy"),nowDay);
			    }
			  }
		} catch (Exception e) {
			// TODO: handle exception
			logger.info(e);
		}
	}
	
	public void mapPut(Map<String, Object> map,Map<String, Map<String, Integer>> personMap,String name) {
		if(map.get("address")!=null || map.get("address").toString() != "") {
			String address = map.get("address").toString();
			if(personMap.containsKey(address)) {
				Map<String, Integer> qMap = personMap.get(address);
				qMap.put(name, qMap.get(name)+1);
				personMap.put(address, qMap);

			} else {
				Map<String, Integer> qMap = new HashMap<String, Integer>();
				qMap.put("qz", 0);
				qMap.put("gfx", 0);
				qMap.put("zy", 0);
				qMap.put(name, 1);
				personMap.put(address, qMap);
			}
		}
	}

	public List<Map<String, Object>> riskAnalysis() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> riskAnalysisList = new ArrayList<Map<String,Object>>();
		String nowDay = DateUtil.getNowDay();
		int gfxFlag = 3;//高风险城市判断
		int zfxFlag = 2;//中风险
		try {
			riskAnalysisList = tjMapper.getTodayData(nowDay);
			ListUtil.ListSortId(riskAnalysisList, "qznum");
			for (Map<String, Object> map : riskAnalysisList) {
				if(Integer.parseInt(map.get("gfxnum").toString()) + Integer.parseInt(map.get("qznum").toString()) >= gfxFlag) {
					map.put("fx", "高风险");
				}else if(Integer.parseInt(map.get("gfxnum").toString()) + Integer.parseInt(map.get("qznum").toString()) >= zfxFlag) {
					map.put("fx","中风险");
				}else {
					map.put("fx", "低风险");
				}
			}

			System.out.println(riskAnalysisList);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return riskAnalysisList;
	}
	/**
	 * 获取被多人接触的节点
	 */
	public List<Map<String, Object>> getDrjcNode(){
		List<Map<String, Object>> personList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> returnPersonList = new ArrayList<Map<String,Object>>();
		try {
			personList = personRepository.getDrjcNode1();
			personList.addAll(personRepository.getDrjcNode2());
			personList.addAll(personRepository.getDrjcNode3());
			ListUtil.ListSortId(personList, "id");
			for (Map<String, Object> map : personList) {
				map.put("flag", 2);
				map.put("shaixuan", 2);
				map.put("jcrs",0);
			}
			for (Map<String, Object> map1 : personList) {		
				if(Integer.parseInt(map1.get("flag").toString()) == 2) {
					map1.put("shaixuan",1);
					for (Map<String, Object> map2 : personList) {
						if(map1.get("id").toString().equals(map2.get("id").toString())) {
							map1.put("jcrs", Integer.parseInt(map1.get("jcrs").toString())+1);
							map2.put("flag", 1);
						}
					}
				}		
			}
			for (Map<String, Object> map : personList) {
				if("1".equals(map.get("shaixuan").toString())) {
					map.put("jcrs", Integer.parseInt(map.get("jcrs").toString())-1);
					if(map.get("glqk") == null || "".equals(map.get("glqk").toString()) || "0".equals(map.get("glqk").toString())) {
						map.put("glqk", "未隔离");
					} else {
						map.put("glqk", "已隔离");
					}
					returnPersonList.add(map);
				}
			}
			ListUtil.ListSortIdTranspose(returnPersonList, "jcrs");
		} catch (Exception e) {
			// TODO: handle exception
			logger.info(e);
		}
		return returnPersonList;
	}
}
